package com.mao.crowd.util;

/** 这个类用于统一ajax请求的返回值类型
 * 封装响应数据，返回是否成功
 * @author maomeng
 * @create 2021-05-27- 16:11
 */
public class ResultEntity<T> {

    public static final String SUCCESS = "SUCCESS"; //
    public static final String FAILED = "FAILED";
    public static final String NO_MESSAGE = "NO_MESSAGE";
    public static final String NO_DATA = "NO_DATA";

//当前请求成功还是失败
    private String result;
//请求失败返回的错误信息
    private String message;
//要返回的数据
    private T data;

    /**
     * 成功且无返回值
     * @param
     * @return
     */
    public static ResultEntity<String> successWithOutData(){
        return new ResultEntity<String>(SUCCESS,NO_MESSAGE,NO_DATA);
    }
    /**
     * 成功有返回值
     * @param <E> 返回值类型
     * @return
     */
    public static <E> ResultEntity<E> successWithData(E data){
        return new ResultEntity<E>(SUCCESS,NO_MESSAGE,data);
    }

    /**
     * 失败并返回错误信息
     * @param message 错误信息
     * @param <E> 返回值类型
     * @return
     */
    public static <E> ResultEntity<E> failed(String message){
        return new ResultEntity<E>(FAILED,message,null);
    }


    public ResultEntity() {
    }

    public ResultEntity(String result, String message, T data) {
        this.result = result;
        this.message = message;
        this.data = data;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "result='" + result + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
